package org.proteored.miapeapi.xml.pride.adapter;

import java.util.List;

import org.apache.log4j.Logger;
import org.proteored.miapeapi.cv.ControlVocabularyManager;
import org.proteored.miapeapi.cv.SampleProcessingStep;
import org.proteored.miapeapi.interfaces.Adapter;
import org.proteored.miapeapi.interfaces.ms.MSAdditionalInformation;
import org.proteored.miapeapi.interfaces.ms.MiapeMSDocument;
import org.proteored.miapeapi.interfaces.msi.MiapeMSIDocument;
import org.proteored.miapeapi.xml.pride.autogenerated.CvParamType;
import org.proteored.miapeapi.xml.pride.autogenerated.ExperimentType.Protocol;
import org.proteored.miapeapi.xml.pride.autogenerated.ExperimentType.Protocol.ProtocolSteps;
import org.proteored.miapeapi.xml.pride.autogenerated.ObjectFactory;
import org.proteored.miapeapi.xml.pride.autogenerated.ParamType;
import org.proteored.miapeapi.xml.pride.util.PrideControlVocabularyXmlFactory;

public class ProtocolAdapter implements Adapter<Protocol> {
	private static Logger log = Logger.getLogger("log4j.logger.org.proteored");

	private final ObjectFactory factory;
	private final MiapeMSDocument miapeMS;
	private final MiapeMSIDocument miapeMSI;
	private final PrideControlVocabularyXmlFactory prideCvUtil;

	private final ControlVocabularyManager cvManager;

	public ProtocolAdapter(ObjectFactory factory,
			ControlVocabularyManager cvManager, MiapeMSDocument miapeMS,
			MiapeMSIDocument miapeMSI) {
		this.factory = factory;
		this.miapeMS = miapeMS;
		this.miapeMSI = miapeMSI;
		this.cvManager = cvManager;
		this.prideCvUtil = new PrideControlVocabularyXmlFactory(factory,
				cvManager);
	}

	@Override
	public Protocol adapt() {
		Protocol protocol = factory.createExperimentTypeProtocol();
		ProtocolSteps protocolSteps = factory
				.createExperimentTypeProtocolProtocolSteps();

		if (miapeMS != null) {
			final List<MSAdditionalInformation> additionalInformations = this.miapeMS
					.getAdditionalInformations();
			if (additionalInformations != null) {
				for (MSAdditionalInformation msAdditionalInformation : additionalInformations) {
					CvParamType cvParam = prideCvUtil.createCvParam(
							msAdditionalInformation.getName(),
							msAdditionalInformation.getValue(),
							SampleProcessingStep.getInstance(cvManager));
					if (cvParam == null
							&& msAdditionalInformation.getName().toLowerCase()
									.contains("digestion")
							&& msAdditionalInformation.getValue() != null
							&& !"".equals(msAdditionalInformation.getValue())) {
						cvParam = prideCvUtil.createCvParam(
								SampleProcessingStep.getInstance(cvManager)
										.getEnzymeDigestionTerm()
										.getPreferredName(),
								msAdditionalInformation.getValue(),
								SampleProcessingStep.getInstance(cvManager));
					}
					if (cvParam != null) {
						ParamType protocolStepsParamType = factory
								.createParamType();
						protocolStepsParamType.getCvParamOrUserParam().add(
								cvParam);
						protocolSteps.getStepDescription().add(
								protocolStepsParamType);
					}
				}
			}
		}
		if (!protocolSteps.getStepDescription().isEmpty()) {
			protocol.setProtocolSteps(protocolSteps);
			// change the name if some step has been captured
			protocol.setProtocolName("Sample preparation protocol");
		} else {
			protocol.setProtocolName("Sample preparation protocol not provided");

		}
		return protocol;
	}
}
